﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// bendiquan 的摘要说明
    /// </summary>
    public class bendiquan : IHttpHandler, IRequiresSessionState
    {
        protected int totalCount = 0;
        public void ProcessRequest(HttpContext context)
        {
            int quanzi_id = DTRequest.GetQueryInt("quanzi_id", 0);
            Utils.WriteTextLog("bendiquan", "quanzi_id:" + quanzi_id , DateTime.Now);
            int type_id = DTRequest.GetQueryInt("type_id", 0);
            ///添加产品分类筛选条件  Demon 2018-11-30
            int category_id = DTRequest.GetQueryInt("category_id", 0);

            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            strTxt.Append("\"scq\":\"" + quanzi_id + "\"");
            //获取信息
            BLL.xinxi xxbll = new BLL.xinxi();
            string strWhere = "id>0";
            if (quanzi_id > 0)
            {
                strWhere += " and quanzi_id=" + quanzi_id;
            }
            if (type_id > 0)
            {
                strWhere += " and type_id=" + type_id;
            }
            ///添加产品分类筛选条件  Demon 2018-11-30
            if (category_id > 0)
            {
                strWhere += " and category_id=" + category_id;
            }
            DataSet xxds = xxbll.GetList(10, 1, strWhere, "add_time desc", out this.totalCount);
            int xx_total_page = this.totalCount / 10;
            strTxt.Append(",\"total_page\":" + xx_total_page.ToString());
            //如果记录存在
            if (xxds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append(",\"xinxi_list\":[");
                for (int i = 0; i < xxds.Tables[0].Rows.Count; i++)
                {
                    if (i>0 && i % 5 == 0)
                    {
                        Model.guanggao adModel = new BLL.guanggao().GetRandModelByQuanzi(quanzi_id);
                        if (adModel != null)
                        {
                            strTxt.Append("{");
                            strTxt.Append("\"typeid\":2");
                            strTxt.Append(",\"id\":" + adModel.id);
                            strTxt.Append(",\"company_id\":\"" + adModel.company_id.ToString() + "\"");
                            strTxt.Append(",\"company_name\":\"" + new BLL.company().GetTitle(adModel.company_id) + "\"");
                            strTxt.Append(",\"adimg\":\"http://pic.shicaiquanzi.com" + adModel.img_url + "\"");
                            strTxt.Append("},");
                        }
                        else
                        {
                            Model.guanggao adModelbb = new BLL.guanggao().GetRandModel();
                            if (adModelbb != null)
                            {
                                strTxt.Append("{");
                                strTxt.Append("\"typeid\":2");
                                strTxt.Append(",\"id\":" + adModelbb.id);
                                strTxt.Append(",\"company_id\":\"" + adModelbb.company_id.ToString() + "\"");
                                strTxt.Append(",\"company_name\":\"" + new BLL.company().GetTitle(adModelbb.company_id) + "\"");
                                strTxt.Append(",\"adimg\":\"http://pic.shicaiquanzi.com" + adModelbb.img_url + "\"");
                                strTxt.Append("},");
                            }
                        }
                    }
                    DataRow dr3 = xxds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"typeid\":1");
                    strTxt.Append(",\"type_id\":" + dr3["type_id"]);
                    string typename = new BLL.xinxi_type().GetTitle(Convert.ToInt32(dr3["type_id"]));
                    if (Convert.ToInt32(dr3["type_id"]) < 3 && typename.Length>=4) { typename = typename.Substring(2); }
                    strTxt.Append(",\"type_name\":\"" + typename + "\"");
                    strTxt.Append(",\"id\":" + dr3["id"]);
                    string remark = dr3["content"].ToString();
                    remark = Utils.DropHTML(remark);
                    int numclock = 150;
                    if (remark.Length > numclock) { remark = remark.Substring(0, numclock) + "..."; }
                    strTxt.Append(",\"remark\":\"" + Microsoft.JScript.GlobalObject.escape(remark) + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr3["add_time"])) + "\"");
                    strTxt.Append(",\"hits\":" + dr3["hits"]);
                    if (dr3["type_id"].ToString() == "3") { strTxt.Append(",\"size\":\"\""); }
                    else { strTxt.Append(",\"size\":\"" + dr3["size_long"] + "×" + dr3["size_wide"] + "×" + dr3["size_height"]  + "mm\""); }
                    strTxt.Append(",\"user_id\":" + dr3["user_id"]);
                    if (Convert.ToInt32(dr3["user_id"]) > 0)
                    {
                        string _username = new BLL.users().GetUserName(Convert.ToInt32(dr3["user_id"]));
                        string _avatar = new BLL.users().GetAvatar(Convert.ToInt32(dr3["user_id"]));
                        string _group_id = new BLL.users().GetGroupID(Convert.ToInt32(dr3["user_id"])).ToString();
                        int userlimit = 16;
                        //if (_group_id == "3") { userlimit = 8; }
                        //else if (_group_id == "2") { userlimit = 10; }
                        if (_username.Length > userlimit) { _username = _username.Substring(0, userlimit) + "..."; }
                        strTxt.Append(",\"user_name\":\"" + _username + "\"");
                        strTxt.Append(",\"group_id\":\"" + _group_id + "\"");
                        if (string.IsNullOrEmpty(_avatar))
                        {
                            strTxt.Append(",\"avatar\":\"../image/default_avatar.png\"");
                        }
                        else
                        {
                            if (_avatar.Contains("shicaiquanzi"))
                            {
                                if (_avatar.Contains("http://"))
                                {
                                    strTxt.Append(",\"avatar\":\"" + _avatar + "\"");
                                }
                                else { strTxt.Append(",\"avatar\":\"http://" + _avatar + "\""); }
                            }
                            else
                            {
                                strTxt.Append(",\"avatar\":\"http://pic.shicaiquanzi.com" + _avatar + "\"");
                            }
                        }
                    }
                    BLL.xinxi_albums pbll = new BLL.xinxi_albums();
                    DataSet pds = pbll.GetLists(int.Parse(dr3["id"].ToString()));
                    if (pds.Tables[0].Rows.Count > 0)
                    {
                        int _count = pds.Tables[0].Rows.Count;
                        if (_count > 4)
                        {
                            _count = 4;
                        }
                        strTxt.Append(",\"pic_list\":[");
                        for (int ii = 0; ii < _count; ii++)
                        {
                            DataRow pdr = pds.Tables[0].Rows[ii];
                            string picurl = pdr["thumb_path"].ToString();
                            if (!picurl.Contains("http://"))
                            {
                                strTxt.Append("{\"picurl\":\"http://" + picurl + "\"}");
                            }
                            else
                            {
                                strTxt.Append("{\"picurl\":\"" + picurl + "\"}");
                            }
                            //else
                            //{
                            //    strTxt.Append("{\"picurl\":\"http://www.shicaiquanzi.com" + pdr["thumb_path"] + "\"}");
                            //}
                            //是否加逗号
                            if (ii < _count - 1)
                            {
                                strTxt.Append(",");
                            }
                        }
                        strTxt.Append("]");
                    }
                    else
                    {
                        strTxt.Append(",\"pic_list\":[]");
                    }
                    //获取评论列表
                    BLL.xinxi_comment cbll = new BLL.xinxi_comment();
                    DataSet cds = cbll.GetList(0, "article_id=" + int.Parse(dr3["id"].ToString()), "id desc");
                    if (cds.Tables[0].Rows.Count > 0)
                    {
                        string c_info = string.Empty;
                        for (int c = 0; c < cds.Tables[0].Rows.Count; c++)
                        {
                            DataRow cdr = cds.Tables[0].Rows[c];
                            int _userid = int.Parse(cdr["user_id"].ToString());
                            string _uname = new BLL.users().GetUserName(_userid);
                            c_info += "<p><strong>" + _uname + "：</strong>" + cdr["remark"] + "</p>";
                        }
                        strTxt.Append(",\"common_info\":\"" + Microsoft.JScript.GlobalObject.escape(c_info) + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"common_info\":\"\"");
                    }

                    strTxt.Append("}");
                    //是否加逗号
                    if (i < xxds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            else
            {
                strTxt.Append(",\"status\":2,\"xinxi_list\":[]");
            }

            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}